package com.jcp.baselibrary.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatDialogFragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.jcp.baselibrary.R;
import com.jcp.baselibrary.utils.UIUtils;

import static android.view.Gravity.CENTER;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

/**
 * =========================================================
 *
 * @ProjectName:
 * @ClassName: BaseDialogFragment
 * @Description: dialog基类
 * @Author: jincp
 * @CreateDate: 2019/3/14 20:09
 * =========================================================
 */
public abstract class BaseDialogFragment extends AppCompatDialogFragment {

    private int mWidth = WRAP_CONTENT;
    private int mHeight = WRAP_CONTENT;
    private int mGravity = CENTER;
    private int mOffsetX = 0;
    private int mOffsetY = 0;
    private float mBackGroundLevel = 0.4f;//背景昏暗度
    private boolean cancelable = true;//点击外部取消
    private int mAnimation = 0;//进入退出动画
    protected DialogResultListener mDialogResultListener;
    protected DialogDismissListener mDialogDismissListener;

    protected static Bundle getArgumentBundle(BaseDialogBuilder b) {
        Bundle bundle = new Bundle();
        bundle.putInt("mWidth", b.mWidth);
        bundle.putInt("mHeight", b.mHeight);
        bundle.putInt("mGravity", b.mGravity);
        bundle.putInt("mOffsetX", b.mOffsetX);
        bundle.putInt("mOffsetY", b.mOffsetY);
        bundle.putFloat("mBackGroundLevel", b.mBackGroundLevel);
        bundle.putInt("mAnimation", b.mAnimation);
        bundle.putBoolean("cancelable", b.mOutCancel);
        return bundle;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mWidth = getArguments().getInt("mWidth");
            mHeight = getArguments().getInt("mHeight");
            mOffsetX = getArguments().getInt("mOffsetX");
            mOffsetY = getArguments().getInt("mOffsetY");
            mBackGroundLevel = getArguments().getFloat("mBackGroundLevel");
            mAnimation = getArguments().getInt("mAnimation");
            mGravity = getArguments().getInt("mGravity");
            cancelable = getArguments().getBoolean("cancelable");
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity(), R.style.Dialog_NoTitle);
        dialog.setCancelable(cancelable);
        dialog.setCanceledOnTouchOutside(cancelable);
        if (!cancelable) {
            dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        return true;
                    }
                    return false;
                }
            });
        }
        return dialog;
    }

    protected abstract void convertView(@Nullable View container, BaseDialogFragment dialogFragment);

    protected abstract int getLayoutId();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        setStyle();
        View contentView = inflater.inflate(getLayoutId(), container, false);
        return contentView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        convertView(view, this);
    }

    /**
     * 设置统一样式
     */
    private void setStyle() {
        Dialog dialog = getDialog();
        if (dialog != null) {
            //获取Window
            Window window = dialog.getWindow();
            if (window != null) {
                // 透明背景
                window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                // 设置宽高
                window.getDecorView().setPadding(0, 0, 0, 0);
                // 设置属性
                WindowManager.LayoutParams layoutParams = window.getAttributes();
                layoutParams.dimAmount = mBackGroundLevel;
                layoutParams.width = mWidth;
                layoutParams.height = mHeight;
                //设置对齐方式
                layoutParams.gravity = mGravity;
                //设置偏移量
                layoutParams.x = UIUtils.dp2px(mOffsetX);
                layoutParams.y = UIUtils.dp2px(mOffsetY);
                //设置动画
                if (mAnimation != 0) {
                    window.setWindowAnimations(mAnimation);
                }
                window.setAttributes(layoutParams);
            }
        }
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        if (mDialogDismissListener != null) {
            mDialogDismissListener.dismiss(this);
        }
    }

    public BaseDialogFragment setDialogResultListener(DialogResultListener dialogResultListener) {
        this.mDialogResultListener = dialogResultListener;
        return this;
    }

    public BaseDialogFragment setDialogDismissListener(DialogDismissListener dialogDismissListener) {
        this.mDialogDismissListener = dialogDismissListener;
        return this;
    }

    /**
     * 点击右边关闭dialog监听
     */
    public class onCloseClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            dismiss();
        }
    }
}

